//
//  SGActivityGroundViewController.m
//  SemiGroup
//
//  Created by 陈无忌 on 12/10/15.
//  Copyright © 2015 陈无忌. All rights reserved.
//

#import "SGActivityGroundViewController.h"
#import "SGActivitySearchViewController.h"

#import "SGActivityGroundTableView.h"

@interface SGActivityGroundViewController ()<SDCycleScrollViewDelegate,SGActivityGroundTableViewDelegate,UIScrollViewDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIView *typeView;
@property (weak, nonatomic) IBOutlet SDCycleScrollView *adsView;
@property (weak, nonatomic) IBOutlet UIScrollView *mainScrollView;
//滑杆
@property (strong, nonatomic) UIView *slideView;

@property (nonatomic) NSInteger selectIndex;

@property (strong, nonatomic) NSArray *adsArray;

@property (strong, nonatomic) SGActivityGroundTableView *groundTableView;
@property (strong, nonatomic) SGActivityGroundTableView *suggestTableView;
@property (strong, nonatomic) SGActivityGroundTableView *histroyTableView;

@end

@implementation SGActivityGroundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.selectIndex = ACTIVITY_GROUND;
    [self mainDisplayViewLoad];
    [self createSlide];
    [self createAds];
    // Do any additional setup after loading the view.
}

- (void)createAds{
    self.adsArray = @[[UIImage imageNamed:@"test_banner_1"],[UIImage imageNamed:@"test_banner_2"]];
    self.adsView.delegate = self;
    self.adsView.infiniteLoop = YES;
    self.adsView.pageControlStyle = SDCycleScrollViewPageContolAlimentCenter;
    self.adsView.autoScrollTimeInterval = 10;
    self.adsView.localizationImagesGroup = self.adsArray;
    self.adsView.dotColor = COLOR_NAVIGATIONTINTCOLOR;
}

- (void)createSlide{
    self.slideView = [[UIView alloc] initWithFrame:CGRectMake(20, self.typeView.frame.size.height - 4, kScreenWidth / 3 - 40, 4)];
    self.slideView.backgroundColor = COLOR_NAVIGATIONTINTCOLOR;
    [self.typeView addSubview:self.slideView];
}

- (void)animationSlide{
    WS
    [UIView animateWithDuration:0.15 animations:^{
        [weakSelf.slideView setFrame:CGRectMake(20 + weakSelf.selectIndex * kScreenWidth / 3, self.typeView.frame.size.height - 4, kScreenWidth / 3 - 40, 4)];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)mainDisplayViewLoad{
    self.mainScrollView.delegate = self;
    
    self.groundTableView = (SGActivityGroundTableView *)[self getViewFromClassName:@"SGActivityGroundTableView"];
    self.groundTableView.selectDelegate = self;
    [self.groundTableView loadDataWithType:ACTIVITY_GROUND];
    [self.mainScrollView addSubview:self.groundTableView];
    WS
    [self.groundTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mainScrollView.mas_left);
        make.top.equalTo(weakSelf.mainScrollView.mas_top);
        make.bottom.equalTo(weakSelf.mainScrollView.mas_bottom);
        make.width.equalTo(weakSelf.mainScrollView.mas_width);
        make.height.equalTo(weakSelf.mainScrollView.mas_height);
    }];
    
    self.suggestTableView = (SGActivityGroundTableView *)[self getViewFromClassName:@"SGActivityGroundTableView"];
    self.suggestTableView.selectDelegate = self;
    [self.suggestTableView loadDataWithType:ACTIVITY_SUGGEST];
    [self.mainScrollView addSubview:self.suggestTableView];
    [self.suggestTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.groundTableView.mas_right);
        make.top.equalTo(weakSelf.mainScrollView.mas_top);
        make.bottom.equalTo(weakSelf.mainScrollView.mas_bottom);
        make.width.equalTo(weakSelf.mainScrollView.mas_width);
        make.height.equalTo(weakSelf.mainScrollView.mas_height);
    }];
    
    self.histroyTableView = (SGActivityGroundTableView *)[self getViewFromClassName:@"SGActivityGroundTableView"];
    self.histroyTableView.selectDelegate = self;
    [self.histroyTableView loadDataWithType:ACTIVITY_HISTROY];
    [self.mainScrollView addSubview:self.histroyTableView];
    [self.histroyTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.suggestTableView.mas_right);
        make.top.equalTo(weakSelf.mainScrollView.mas_top);
        make.bottom.equalTo(weakSelf.mainScrollView.mas_bottom);
        make.right.equalTo(weakSelf.mainScrollView.mas_right);
        make.width.equalTo(weakSelf.mainScrollView.mas_width);
        make.height.equalTo(weakSelf.mainScrollView.mas_height);
    }];
    self.mainScrollView.pagingEnabled = YES;
    self.mainScrollView.showsHorizontalScrollIndicator = NO;
    
    
}

- (UIView *)getViewFromClassName:(NSString *)name{
    NSArray *views = [[NSBundle mainBundle] loadNibNamed:name owner:nil options:nil];
    for (UIView *view in views) {
        if ([view isKindOfClass:NSClassFromString(name)]) {
            return view;
        }
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Scroll view delegate

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if ([scrollView isEqual:self.mainScrollView]) {
        if (!decelerate) {
            self.selectIndex = (scrollView.contentOffset.x + scrollView.frame.size.width * 0.5)/ scrollView.frame.size.width;
            [self animationSlide];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.mainScrollView]) {
        self.selectIndex = (scrollView.contentOffset.x + scrollView.frame.size.width * 0.5) / scrollView.frame.size.width;
        [self animationSlide];
    }
}

#pragma mark - Custom IBAction
- (IBAction)choseDisplayTpyeClick:(UIButton *)sender {
    self.selectIndex = sender.tag;
    [self animationSlide];
    [self.mainScrollView scrollRectToVisible:CGRectMake(self.selectIndex *kScreenWidth, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height) animated:YES];
//    [self.scrollView setContentOffset:CGPointMake(index * kScreenWidth, 0)];
}

#pragma mark - CycScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"select ads --> %d",index);
}

#pragma mark - Activity Table View delegate
- (void)activitySelect:(SGActivity *)activity{
    
}


@end
